program tangqiao;

  type long=string[4];

       kkk=^jjj;
       jjj=record
             shu:long;
             link:kkk;
           end;

  var head,p,q,t:kkk;
      i,j,n:integer;

function check(in_shu,s:long;a,b:shortint):boolean;
  var i,j:integer;
      numa,numb:integer;
  begin
    numa:=0;numb:=0;
    for i:=1 to 4 do
      begin
        for j:=1 to 4 do
          begin
            if s[j]=in_shu[i] then
              begin
                if i=j then inc(numa)
                       else inc(numb);

              end;
          end;
      end;
    if (numa=a) and (numb=b) then check:=true
                             else check:=false;

  end;

function ten(x:integer):integer;
  var t,i,j:integer;
  begin
    t:=1;
    for i:=1 to x do
      t:=t*10;
    ten:=t;
  end;

procedure search;
  var i,j,pt,huan_i,code:integer;
      ss:array[1..5] of shortint;
      shu:set of 0..9;
      huan_s:string[4];
begin
  { init }
  fillchar(ss,sizeof(ss),0);
  pt:=1;ss[pt]:=-1;
  shu:=[0..9];
  n:=0;
  { init }

  while pt>0 do
    begin
      inc(ss[pt]);
      if pt>4 then
        begin
          dec(pt);
          shu:=shu+[ss[pt]];

          {add new information}
          huan_i:=0;
          for i:=1 to 4 do
            begin
              huan_i:=huan_i+ss[i]*ten(4-i);
            end;
          str(huan_i,huan_s);
          if length(huan_s)<>4 then huan_s:='0'+huan_s;

          inc(n);
          new(p);
          if n=1 then
            begin
              head:=p;
              q:=p;
              p^.shu:=huan_s;
              p^.link:=nil;
            end
                 else
            begin
              new(p);
              p^.shu:=huan_s;
              p^.link:=nil;
              q^.link:=p;

              q:=p;
            end;
        end
              else
        begin
          if ss[pt]>9 then
            begin
              dec(pt);
              shu:=shu+[ss[pt]];
            end
                      else
            begin
              if ss[pt] in shu then
                begin
                  shu:=shu-[ss[pt]];
                  inc(pt);
                  ss[pt]:=-1;
                end;
            end;
        end;
    end;

end;

procedure firstpan(in_shu:long);
  var a,b:shortint;
  begin
    writeln('Now I am guessing...   ',in_shu);
    readln(a,b);
    while not check(in_shu,head^.shu,a,b) do
      begin
        if (head^.shu='')or(head=nil) then
          begin
            writeln('Sorry,there is no answer .You must be inputing error .');
            halt;
          end;
        p:=head;
        head:=head^.link;
      {  dispose(p);   }
      end;

    p:=head^.link;
    q:=head;
    repeat
      if not check(in_shu,p^.shu,a,b) then
         begin
           if (p=nil)or(p^.link=nil) then
             begin
               q^.link:=nil;
             end
                    else
             begin
               t:=p;
               p:=p^.link;
              { dispose(t);  }
               q^.link:=p;
             end;
         end
                                 else
         begin
           p:=p^.link;
           q:=q^.link;
         end;
    until q^.link=nil;
  end;

{
procedure output;
  var f:text;
      n:longint;
      name:string;
      p:kkk;
  begin
    inc(i);
    n:=1;
    str(i,name);
    name:='e:\tt\test'+name+'.txt';
    assign(f,name);
    rewrite(f);
    p:=head;
    while p^.link<>nil do
      begin
        write(f,p^.shu,'  ');
        p:=p^.link;
        inc(n);
      end;
    writeln(f,p^.shu);
    write(f,'The total number is :',n);
    close(f);
  end;   }

begin
  search;
  firstpan('0123');
  firstpan('4567');
{ firstpan('6789');   }
  while head^.link<>nil do
    begin
      firstpan(head^.shu);
    end;

  writeln('The answer is : ',head^.shu);
end.


